﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Validation;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace KPIS.GERP.WCF.ePayment.Models
{
    [DataContract, Table("Supplier")]
    public class Supplier
    {
        public Supplier()
        {
            deleted = false;
            this.Invoices = new HashSet<Invoice>();
        }

        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [DataMember, Display(Name = "supplier_id")]
        public int supplier_id { get; set; }

        [DataMember, Display(Name = "เลขประจำตัวผู้เสียภาษี")]
        [MaxLength(20)]
        [Required]
        public string supplier_tax_code { get; set; }

        [DataMember, Display(Name = "เลขสาขา")]
        [MaxLength(10)]
        [Required]
        public string supplier_branch_code { get; set; }

        [DataMember, Display(Name = "ชื่อ")]
        [MaxLength(140)]
        [Required]
        public string supplier_name { get; set; }

        [DataMember, Display(Name = "ประเภทธุรกิจ")]
        [Required]
        public int business_type { get; set; }

        [DataMember, Display(Name = "เลขที่บัญชีเงินฝากธนาคารที่ใช้โอน")]
        [MaxLength(20)]
        public string bank_account_code { get; set; }

        [DataMember, Display(Name = "ที่อยู่")]
        public string address { get; set; }

        [DataMember, Display(Name = "หมายเลขโทรศัพท์")]
        [MaxLength(30)]
        public string tel { get; set; }

        [DataMember, Display(Name = "หมายเลขโทรสาร")]
        [MaxLength(30)]
        public string fax { get; set; }

        [DataMember, Display(Name = "วันที่สร้างรายการ")]
        [Required]
        public DateTime created_when { get; set; }

        [DataMember, Display(Name = "วันที่แก้ไขรายการ")]
        public DateTime? updated_when { get; set; }

        [DataMember, Display(Name = "ลบ")]
        public bool deleted { get; set; }

        [DataMember, Display(Name = "วันที่ลบรายการ")]
        public DateTime? deleted_when { get; set; }

        public virtual ICollection<Invoice> Invoices { get; set; }

        public int Create(Supplier model)
        {
            using(ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.deleted = false;
                    model.created_when = DateTime.Now;

                    context.Suppliers.Add(model);
                    context.SaveChanges();

                    return model.supplier_id;
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }

        public void Edit(Supplier model)
        {
            using (ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.updated_when = DateTime.Now;

                    context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }

        public void Delete(Supplier model)
        {
            using (ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.deleted_when = DateTime.Now;
                    model.deleted = true;

                    context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }
    }
}